package com.trkj.push;

import android.app.Notification;
import android.content.Context;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.baidu.android.pushservice.CustomPushNotificationBuilder;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.trkj.base.User;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.util.List;

/**
 * 消息推送
 * Created by LiZanhong on 2015/07/07.
 */
public class Push {
    private Context context;

    public Push(Context context) {
        this.context = context;
    }

    /**
     * 启动推送
     */
    public void startPush() {
        String apiKey = Utils.getMetaValue(context, "api_key");
        Log.d("apiKey", apiKey);
        PushManager.startWork(context.getApplicationContext(),
            PushConstants.LOGIN_TYPE_API_KEY, apiKey);

        //设置通知
        CustomPushNotificationBuilder cBuilder = new CustomPushNotificationBuilder(
                R.layout.notification_custom_builder, R.id.notification_icon,
                R.id.notification_title, R.id.notification_text);

        cBuilder.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);
        cBuilder.setNotificationDefaults(Notification.DEFAULT_VIBRATE);
        cBuilder.setStatusbarIcon(context.getApplicationInfo().icon);
        cBuilder.setLayoutDrawable(R.drawable.simple_logo);
        cBuilder.setNotificationSound(Uri.withAppendedPath(
                MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "6").toString());
        // 推送高级设置，通知栏样式设置为下面的ID
        PushManager.setNotificationBuilder(context, 1, cBuilder);
    }

    /**
     * 设置标签，将当前用户所在的部门名称作为标签，方便按部门推送
     */
    public void setTags(){
        User user = UserService.getInstance(context).parseUser();
        if(user != null) {
            String deptName = user.deptName;

            if (deptName != null) {
                List<String> tags = Utils.getTagsList(deptName);
                PushManager.setTags(context.getApplicationContext(), tags);
            }
        }
    }



}
